package com.zat.mes.andrmes.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期工具类(未特别说明的均为系统默认时区下的时间)
 */
public class DateUtil {
    /**
     * 1s==1000ms
     */
    private final static int TIME_MILLISECONDS = 1000;
    /**
     * 时间中的分、秒最大值均为60
     */
    private final static int TIME_NUMBERS = 60;
    /**
     * 时间中的小时最大值
     */
    private final static int TIME_HOURSES = 24;
    /**
     * 格式化日期的标准字符串
     */
    public final static String FORMAT = "yyyy-MM-dd HH:mm:ss";

    public final static String FORMAT_2 = "yyyy-MM-dd HH:ss";
    public final static String FORMAT_3 = "yyyy-MM-dd";
    public final static String FORMAT_b = "yyyy/MM/dd";
    public final static String FORMAT_s = "yyyy/MM/dd HH:mm";
    public final static String FORMAT_y = "yyyy";
    public final static String FORMAT_z = "yyyy年MM月dd日";

    /**
     * 获取时区信息
     */
    public static TimeZone getTimeZone() {
        return TimeZone.getDefault();
    }

    /**
     * 将日期字符串转换为Date对象
     *
     * @param date 日期字符串，必须为"yyyy-MM-dd HH:mm:ss"
     * @return 日期字符串的Date对象表达形式
     */
    public static Date parseDate(String date) {
        return parseDate(date, FORMAT);
    }

    /**
     * 将日期字符串转换为Date对象
     *
     * @param date   日期字符串，必须为"yyyy-MM-dd HH:mm:ss"
     * @param format 格式化字符串
     * @return 日期字符串的Date对象表达形式
     */
    public static Date parseDate(String date, String format) {
        Date dt = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            dt = dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dt;
    }

    /**
     * 将Date对象转换为指定格式的字符串
     *
     * @param date Date对象
     * @return Date对象的字符串表达形式"yyyy-MM-dd HH:mm:ss"
     */
    public static String formatDate(Date date) {
        return formatDate(date, FORMAT);
    }

    /**
     * 将Date对象转换为指定格式的字符串
     *
     * @param date Date对象
     * @return Date对象的字符串表达形式
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 格式化日期
     *
     * @return 日期字符串"yyyy-MM-dd HH:mm:ss"
     */
    public static String formatUnixTime(long unixTime) {
        return formatUnixTime(unixTime, FORMAT);
    }

    /**
     * 格式化日期
     *
     * @return 日期字符串
     */
    public static String formatUnixTime(long unixTime, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(unixTime);
    }

    public static String format(long unixTime) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT);
        return dateFormat.format(new Date(unixTime * 1000L));
    }

    public static String format13Date(long unixTime) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT);
        return dateFormat.format(new Date(unixTime));
    }

    /**
     * 将GMT日期格式化为系统默认时区的日期字符串表达形式
     *
     * @param gmtUnixTime GTM时间戳
     * @return 日期字符串"yyyy-MM-dd HH:mm:ss"
     */
    public static String formatGMTUnixTime(long gmtUnixTime) {
        return formatGMTUnixTime(gmtUnixTime, FORMAT);
    }

    /**
     * 将GMT日期格式化为系统默认时区的日期字符串表达形式
     *
     * @param gmtUnixTime GTM时间戳
     * @param format      格式化字符串
     * @return 日期字符串"yyyy-MM-dd HH:mm:ss"
     */
    public static String formatGMTUnixTime(long gmtUnixTime, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(gmtUnixTime
                + TimeZone.getDefault().getRawOffset());
    }

    /**
     * 获取时间戳的Date表示形式
     *
     * @param unixTime unix时间戳
     * @return Date对象
     */
    public static Date getDate(long unixTime) {
        return new Date(unixTime);
    }

    /**
     * 获取GMT时间戳的Date表示形式（转换为Date表示形式后，为系统默认时区下的时间）
     *
     * @param gmtUnixTime GMT Unix时间戳
     * @return Date对象
     */
    public static Date getGMTDate(long gmtUnixTime) {
        return new Date(gmtUnixTime + TimeZone.getDefault().getRawOffset());
    }

    /**
     * 将系统默认时区的Unix时间戳转换为GMT Unix时间戳
     *
     * @param unixTime unix时间戳
     * @return GMT Unix时间戳
     */
    public static long getGMTUnixTime(long unixTime) {
        return unixTime - TimeZone.getDefault().getRawOffset();
    }

    /**
     * 将GMT Unix时间戳转换为系统默认时区的Unix时间戳
     *
     * @param gmtUnixTime GMT Unix时间戳
     * @return 系统默认时区的Unix时间戳
     */
    public static long getCurrentTimeZoneUnixTime(long gmtUnixTime) {
        return gmtUnixTime + TimeZone.getDefault().getRawOffset();
    }

    /**
     * 获取当前时间的GMT Unix时间戳
     *
     * @return 当前的GMT Unix时间戳
     */
    public static long getGMTUnixTimeByCalendar() {
        Calendar calendar = Calendar.getInstance();
        // 获取当前时区下日期时间对应的时间戳
        long unixTime = calendar.getTimeInMillis();
        // 获取标准格林尼治时间下日期时间对应的时间戳
        long unixTimeGMT = unixTime - TimeZone.getDefault().getRawOffset();
        return unixTimeGMT;
    }

    /**
     * 获取当前时间的Unix时间戳
     *
     * @return 当前的Unix时间戳
     */
    public static long getUnixTimeByCalendar() {
        Calendar calendar = Calendar.getInstance();
        // 获取当前时区下日期时间对应的时间戳
        long unixTime = calendar.getTimeInMillis();
        return unixTime;
    }

    /**
     * 获取更改时区后的时间
     *
     * @param date    时间
     * @param oldZone 旧时区
     * @param newZone 新时区
     * @return 时间
     */
    public static Date changeTimeZone(Date date, TimeZone oldZone,
                                      TimeZone newZone) {
        Date dateTmp = null;
        if (date != null) {
            int timeOffset = oldZone.getRawOffset() - newZone.getRawOffset();
            dateTmp = new Date(date.getTime() - timeOffset);
        }
        return dateTmp;
    }

    /**
     * 将总秒数转换为时分秒表达形式
     *
     * @param seconds 任意秒数
     * @return %s小时%s分%s秒
     */
    public static String formatTime(long seconds) {
        long hh = seconds / TIME_NUMBERS / TIME_NUMBERS;
        long mm = (seconds - hh * TIME_NUMBERS * TIME_NUMBERS) > 0 ? (seconds - hh
                * TIME_NUMBERS * TIME_NUMBERS)
                / TIME_NUMBERS
                : 0;
        long ss = seconds < TIME_NUMBERS ? seconds : seconds % TIME_NUMBERS;
        return (hh == 0 ? "" : (hh < 10 ? "0" + hh : hh) + "小时")
                + (mm == 0 ? "" : (mm < 10 ? "0" + mm : mm) + "分")
                + (ss == 0 ? "" : (ss < 10 ? "0" + ss : ss) + "秒");
    }

    // public static String formatTimeMills(String timeMills) {
    // if (!TextUtils.isEmpty(timeMills)) {
    // long value = Long.parseLong(timeMills) * 1000L;
    // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    // String format = sdf.format(value);
    // String[] split = format.split("-");
    // }
    // }

    /**
     * 获取当前时间距离指定日期时差的大致表达形式
     *
     * @return 时差的大致表达形式
     */
    public static String getDiffTime(long date) {
        String strTime = "很久很久以前";
        long time = Math.abs(new Date().getTime() - date);
        // 一分钟以内
        if (time < TIME_NUMBERS * TIME_MILLISECONDS) {
            strTime = "刚刚";
        } else {
            int min = (int) (time / TIME_MILLISECONDS / TIME_NUMBERS);
            if (min < TIME_NUMBERS) {
                if (min < 15) {
                    strTime = "一刻钟前";
                } else if (min < 30) {
                    strTime = "半小时前";
                } else {
                    strTime = "1小时前";
                }
            } else {
                int hh = min / TIME_NUMBERS;
                if (hh < TIME_HOURSES) {
                    strTime = hh + "小时前";
                } else {
                    int days = hh / TIME_HOURSES;
                    if (days <= 6) {
                        strTime = days + "天前";
                    } else {
                        int weeks = days / 7;
                        if (weeks < 3) {
                            strTime = weeks + "周前";
                        }
                    }
                }
            }
        }

        return strTime;
    }

    /**
     * 获取当前时间距离指定日期时差的大致表达形式
     *
     * @return 时差的大致表达形式
     */
    public static String getDiffTimeForHome(long date) {
        String strTime = "";
        long time = Math.abs(new Date().getTime() - date);
        // 一分钟以内
        if (time < TIME_NUMBERS * TIME_MILLISECONDS) {
            strTime = "刚刚";
        } else {
            int min = (int) (time / TIME_MILLISECONDS / TIME_NUMBERS);
            if (min < TIME_NUMBERS) {
                strTime = min + "分钟前";
            } else {
                int hh = min / TIME_NUMBERS;
                if (hh < TIME_HOURSES) {
                    strTime = hh + "小时前";
                } else {
                    int days = hh / TIME_HOURSES;
                    if (days <= 6) {
                        strTime = days + "天前";
                    } else {
                        strTime = formatUnixTime(date, "yyyy-MM-dd");
                    }
                }
            }
        }
        return strTime;
    }

    public static Long getDaysBetween(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (toCalendar.getTime().getTime() - fromCalendar.getTime()
                .getTime()) / (1000 * 60 * 60 * 24);
    }

    /**
     * unix时间戳转换为北京时间
     */

    public static String TimeStamp2Date(String timestampString, String formats) {
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat(formats)
                .format(new Date(timestamp));
        return date;
    }

    public static boolean isNewYearTime() {
        long nowUnixTime = System.currentTimeMillis() / 1000L;
        long newYearStart = 1485360000L; // 开始时间2017/01/26 00:00:00 1485360000
        long newYearStop = 1486828800L;// 结束时间 2017/02/12 00:00:00 1486828800
        if (newYearStart <= nowUnixTime && nowUnixTime <= newYearStop) {
            return true;
        } else {
            return false;// false
        }
    }

    /**
     * 将一个时间戳转换成提示性时间字符串，如今天 ，昨天
     *
     * @param timeStamp
     * @return 13位 unix
     */
    public static String convertTimeToFormat(long timeStamp, String formart) {
        // long nowTime =System.currentTimeMillis()/1000 ; //当前的系统时间 秒数
        String beforeYestoday = getBeforYestodayDate();
        String yestoday = getYestoryDate();
        String today = getTodayDate();
        String timeStampDay = getTimeForMat(timeStamp);

        if (timeStampDay.equals(today)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(formart);
            return "今天" + dateFormat.format(new Date(timeStamp));
        } else if (timeStampDay.equals(yestoday)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(formart);
            return "昨天" + dateFormat.format(new Date(timeStamp));
        } else if (timeStampDay.equals(beforeYestoday)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(formart);
            return "前天" + dateFormat.format(new Date(timeStamp));
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_2);
            return dateFormat.format(new Date(timeStamp));
        }
    }

    public static String getTimeForMat(long timeStamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(new Date(timeStamp));
    }

    /**
     * 得到前天的日期
     *
     * @return
     */
    public static String getBeforYestodayDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String beforYestoday = sdf.format(calendar.getTime());
        return beforYestoday;
    }

    /**
     * 得到昨天的日期
     *
     * @return
     */
    public static String getYestoryDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String yestoday = sdf.format(calendar.getTime());
        return yestoday;
    }

    /**
     * 得到今天的日期
     *
     * @return
     */
    public static String getTodayDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        return date;
    }

    /**
     * 对比当前日期 和 保存的日期对比大小
     */
    public static boolean compareToSaveTime(String saveTime) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowtime = format.format(date);
        try {
            Date saveDate = format.parse(saveTime);
            Date nowDate = format.parse(nowtime);
            if (nowDate.getTime() > saveDate.getTime()) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return true;
    }

    /**
     * 获取当前系统日期
     */
    public static String getNowTime() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowtime = format.format(date);
        return nowtime;
    }

    /**
     * 星期几
     */
    public static String getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (i < 0) {
            i = 0;
        }
        switch (i) {
            case 0:
                return "星期日";
            case 1:
                return "星期一";
            case 2:
                return "星期二";
            case 3:
                return "星期三";
            case 4:
                return "星期四";
            case 5:
                return "星期五";
            case 6:
                return "星期六";
        }

        return "";
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间 "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsToday(long day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = new Date(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return boolean 返回类型
     * @throws
     * @Title: sameDay
     * @说 明:判断是否是同一天
     * @参 数: @param day
     * @参 数: @param shareDay
     * @参 数: @return
     */
    public static boolean sameDay(long day, long shareDay) {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(day);
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = new Date(shareDay);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 小于
     */
    public static boolean lessOneDay(long day, long shareDay) {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(day);
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = new Date(shareDay);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = pre.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR);

            if (diffDay <= 0) {
                return true;
            }
        } else if (pre.get(Calendar.YEAR) < cal.get(Calendar.YEAR)) {
            return true;
        }
        return false;
    }

    /**
     * 判断今天是否过来某个时间点
     */
    public static boolean afterTheHour(long systemTime, int hour, int minute) {
        Date date = new Date(systemTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int systemHour = calendar.get(Calendar.HOUR_OF_DAY);
        int systemMinute = calendar.get(Calendar.MINUTE);
        if (systemHour > hour) {
            return true;
        } else if (systemHour == hour && systemMinute > minute) {
            return true;
        }
        return false;
    }

    /**
     * 获取当前年份
     */
    public static String getYear() {
        Calendar calendar = Calendar.getInstance().getInstance();
        return calendar.get(Calendar.YEAR) + "";
    }

    /**
     * 获取当前月份
     */
    public static String getMonth() {
        Calendar calendar = Calendar.getInstance().getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month < 10) {
            return "0" + (calendar.get(Calendar.MONTH) + 1);
        }
        return month + "";
    }

    /**
     * 获取当日期
     */
    public static String getDay() {
        Calendar calendar = Calendar.getInstance().getInstance();
        return calendar.get(Calendar.DATE) + "";
    }

    /**
     * 获取当前星期
     */
    public static String getWeek() {
        String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance().getInstance();
        int week = (calendar.get(Calendar.DAY_OF_WEEK) - 1);
        return weeks[week];
    }

    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 加一天
     *
     * @param date
     * @return
     */
    public static Date getOnMoreDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, 1);
        date = c.getTime();
        return date;
    }
}